<?php

/*************
 * Constants *
 *************/

// Absolute path to output html page.
$OUT_HTML_PATH = "/var/www/html/cc/index.html";

// Absolute path to cache file.
$CACHE_FILE_PATH = "/var/www/html/cc/cache";

// Update rate in minutes.
$UPDATE_RATE_MIN = '25';

// Special persons for last post search.
$AUTHORS = array(array('alt' => 'Catie', 'trip' => '!<b>catie.</b>'),
                 array('alt' => 'Pocky', 'trip' => '!<b>pocky.</b>'),
                 array('alt' => 'Boxxmom', 'trip' => '!<b>boxxmom.</b>'),
                 array('alt' => 'Nick', 'trip' => '!<b>nick.</b>'));

$TAB = '    ';

/*************
 * Functions *
 *************/

/**
 * Gets web page refered by URL. Uses random execution delay 10-15 seconds.
 * @param string $url URL.
 * @return string
 * page content.
 */
function url_get_contents($url) {
    $USER_AGENTS = array('Catie Checker version=2,home=http://code.google.com/p/catie-checker/');
    sleep(rand(10, 15));
    $crl = curl_init();
    curl_setopt($crl, CURLOPT_URL, $url);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($crl,
                CURLOPT_USERAGENT,
                $USER_AGENTS[rand(0, count($USER_AGENTS) - 1)]);
    $html = curl_exec($crl);
    curl_close($crl);
    return $html;
}
/**
 * Returns boards list. Boards sets manually.
 * @return array
 * boards list.
 */
function get_boards() {
    return array(array('name' => 'b',
                       'link' => 'http://chan.catiewayne.com/b/',
                       'pages' => array(array('threads' => array()))),
                 array('name' => 'c',
                       'link' => 'http://chan.catiewayne.com/c/',
                       'pages' => array(array('threads' => array()))),
                 array('name' => 'p',
                       'link' => 'http://chan.catiewayne.com/p/',
                       'pages' => array(array('threads' => array()))),
                 array('name' => 'o',
                       'link' => 'http://chan.catiewayne.com/o/',
                       'pages' => array(array('threads' => array()))),
                 array('name' => 'm',
                       'link' => 'http://chan.catiewayne.com/m/',
                       'pages' => array(array('threads' => array()))),
                 array('name' => 'nc',
                       'link' => 'http://chan.catiewayne.com/nc/',
                       'pages' => array(array('threads' => array()))));
}
/**
 * Parses board page and return threads and posts.
 * @param string $html HTML-code of board page.
 * @return array
 * threads.
 */
function parse_board_preview_page($html) {
    $threads = array();

    // Dispel new lines and tabs.
    $_html = preg_replace("/[\n\r\t]/", '', $html);

    // Split html-code into threads.
    $threads_html = preg_split('/<div id="thread(\\d+)(\\w+)">/',
                               $_html,
                               -1,
                               PREG_SPLIT_DELIM_CAPTURE);

    // $i = 1, 4, 7, 10, ...
    // 
    // $i - thread number.
    // $i + 1 - board name.
    // $i + 2 - html-code of thread.
    for ($i = 1; $i < count($threads_html); $i += 3) {
        $posts = array();

        // Split each html-code of thread into OP and replies.
        list($op, $r) = preg_split("/<div id=\"replies"
                                       ."{$threads_html[$i]}"
                                       ."{$threads_html[$i + 1]}\">/",
                                   $threads_html[$i + 2],
                                   -1,
                                   PREG_SPLIT_DELIM_CAPTURE);

        // Parse op name, trip and date.
        preg_match_all('/<span class="postername">(.*?)<\/span>/',
                       $op,
                       $opname);
        preg_match_all('/<span class="postertrip">(.+?)<\/span>/',
                       $op,
                       $optrip);
        preg_match_all("/(\\d+?)\/(\\d+)\/(\\d+)\(\\w+\)(\\d+):(\\d+)"
                           ."<\/label>/",
                       $op,
                       $opdate);

        $date = new DateTime("20{$opdate[1][0]}-{$opdate[2][0]}-{$opdate[3][0]}"
                            ." {$opdate[4][0]}:{$opdate[5][0]}");

        // Add op post.
        array_push($posts,
                   array('number' => $threads_html[$i],
                         'name' => $opname[1][0],
                         'trip' => (isset($optrip[1][0]) ? $optrip[1][0] : ''),
                         'date' => $date->format('U'),
                         'link' => "http://chan.catiewayne.com/"
                                  ."{$threads_html[$i + 1]}/res/{$threads_html[$i]}"
                                  .".html#{$threads_html[$i]}"));

        // Split replies html-code into posts.
        $replies_html = preg_split('/<td class="reply" id="reply(\\d+)">/',
                                   $r,
                                   -1,
                                   PREG_SPLIT_DELIM_CAPTURE);

        // Check if thread have omited posts, calculate count of omited posts.
        $omited = preg_match('/omitted/', $replies_html[0]);
        $omitedn = 0;
        if ($omited) {
            preg_match_all('/<span class="omittedposts">(\\d+)/',
                           $replies_html[0],
                           $omited_count);
            $omitedn = $omited_count[1][0];
        }

        // Check if thread sticked.
        $sticked = preg_match('/alt="Stickied"/', $op);

        // $j = 1, 3, 5, 7, ...
        //
        // $j - post number.
        // $j + 1 - post html-code.
        for ($j = 1; $j < count($replies_html); $j += 2) {

            // Parse reply name, trip and date.
            preg_match_all('/<span class="postername">(.*?)<\/span>/',
                           $replies_html[$j + 1],
                           $rname);
            preg_match_all('/<span class="postertrip">(.+?)<\/span>/',
                           $replies_html[$j + 1],
                           $rtrip);
            preg_match_all("/(\\d+?)\/(\\d+)\/(\\d+)\(\\w+\)(\\d+):(\\d+)<\/label>/",
                           $replies_html[$j + 1],
                           $rdate);

            $date = new DateTime("20{$rdate[1][0]}-{$rdate[2][0]}"
                                 ."-{$rdate[3][0]}"
                                 ." {$rdate[4][0]}:{$rdate[5][0]}");

            // Add reply.
            array_push($posts,
                       array('number' => $replies_html[$j],
                             'name' => $rname[1][0],
                             'trip' => (isset($rtrip[1][0])
                                        ? $rtrip[1][0]
                                        : ''),
                             'date' => $date->format('U'),
                             'link' => "http://chan.catiewayne.com/"
                                      ."{$threads_html[$i + 1]}/res/"
                                      ."{$threads_html[$i]}.html#"
                                      ."{$replies_html[$j]}"));
        }

        // Add thread.
        array_push($threads,
                   array('number' => $threads_html[$i],
                         'board' => $threads_html[$i + 1],
                         'omited' => $omited,
                         'omitedn' => $omitedn,
                         'sticked' => $sticked,
                         'link' => "http://chan.catiewayne.com/"
                                   ."{$threads_html[$i + 1]}/res/"
                                   ."{$threads_html[$i]}.html",
                         'posts' => $posts));
    }

    return $threads;
}
/**
 * Parses thread page and return posts.
 * @param string $html HTML-code of thread page.
 * @return array
 * posts.
 */
function parse_thread_view_page($html) {
    $posts = array();

    // Dispel new lines and tabs.
    $_html = preg_replace("/[\n\r\t]/", '', $html);

    // Split thread html-code into posts.
    $posts_html = preg_split('/<td class="reply" id="reply(\\d+)">/',
                             $_html,
                             -1,
                             PREG_SPLIT_DELIM_CAPTURE);

    // Parse thread number and board name.
    preg_match_all('/<div id="thread(\\d+)(\\w+)">/',
                   $posts_html[0],
                   $thread_info);

    for ($j = 1; $j < count($posts_html); $j += 2) {

        // Parse reply name, trip and date.
        preg_match_all('/<span class="postername">(.*?)<\/span>/',
                       $posts_html[$j + 1],
                       $rname);
        preg_match_all('/<span class="postertrip">(.+?)<\/span>/',
                       $posts_html[$j + 1],
                       $rtrip);
        preg_match_all("/(\\d+?)\/(\\d+)\/(\\d+)\(\\w+\)(\\d+):(\\d+)<\/label>/",
                       $posts_html[$j + 1],
                       $rdate);

        $date = new DateTime("20{$rdate[1][0]}-{$rdate[2][0]}-{$rdate[3][0]}"
                            ." {$rdate[4][0]}:{$rdate[5][0]}");

        // Add post.
        array_push($posts,
                   array('number' => $posts_html[$j],
                         'name' => $rname[1][0],
                         'trip' => (isset($rtrip[1][0]) ? $rtrip[1][0] : ''),
                         'date' => $date->format('U'),
                         'link' => "http://chan.catiewayne.com/"
                                  ."{$thread_info[1][0]}/res/"
                                  ."{$thread_info[2][0]}.html#"
                                  ."{$posts_html[$j]}"));
    }

    return $posts;
}
/**
 * Returns current date formated as described in RFC 2822.<br>
 * For example: Sun, 13 Mar 2011 01:39:03 -0800
 * @return string
 * current date.
 */
function cur_date() {
    return date("r ");
}
/**
 * Adds new threads and new posts to old threads.
 * @param array $threads Old threads, cached.
 * @param array $new_threads New threads.
 * @return array
 * 0 - count of added new threads.
 * 1 - count of added new posts.
 */
function add_new_threads(&$threads, $new_threads) {
    $threads_added = 0;
    $posts_added = 0;

    for ($i = 0; $i < count($new_threads); $i++) {
        $found = FALSE;

        for ($j = 0; $j < count($threads); $j++) {

            // Thread already in old threads (in cache). We need update it.
            if ($threads[$j]['number'] == $new_threads[$i]['number']
                    && $threads[$j]['board'] == $new_threads[$i]['board']) {

                // Check if thread what already in cache has a new posts.
                for ($k = 0; $k < count($new_threads[$i]['posts']); $k++) {
                    $found = FALSE;

                    for ($l = 0; $l < count($threads[$j]['posts']); $l++) {

                        // Post already in old thread (cached).
                        if ($threads[$j]['posts'][$l]['number']
                                == $new_threads[$i]['posts'][$k]['number']) {
                            $found = TRUE;
                            break;
                        }
                    }
                    if (!$found) {

                        // Post is new. Add it to old thread (to cache).
                        array_push($threads[$j]['posts'],
                                   $new_threads[$i]['posts'][$k]);
                        $posts_added++;
                    }
                }

                // Update count of omited posts.
                $threads[$j]['omitedn'] = $new_threads[$i]['omitedn'];

                $found = TRUE;
                break;
            }
        }
        if (!$found) {

            // Thread not cached yet so just add it.
            array_push($threads, $new_threads[$i]);
            $threads_added++;
            $posts_added += count($new_threads[$i]['posts']);
        }
    }

    return array($threads_added, $posts_added);
}
/**
 * Adds new posts to old posts.
 * @param array $posts Old posts, cached.
 * @param array $new_posts New posts.
 * @return int
 * count of added new posts.
 */
function add_new_posts(&$posts, $new_posts) {
    $posts_added = 0;

    for ($i = 0; $i < count($new_posts); $i++) {
        $found = FALSE;

        for ($j = 0; $j < count($posts); $j++) {
            if ($posts[$j]['number'] == $new_posts[$i]['number']) {
                $found = TRUE;
                break;
            }
        }
        if (!$found) {

            // Post not cached yet so just add it.
            array_push($posts, $new_posts[$i]);
            $posts_added++;
        }
    }

    return $posts_added;
}
/**
 * Calculates posts count in cache.
 * @param array $boards Cached data.
 * @return int
 * posts count.
 */
function get_posts_count($boards) {
    $posts_count = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    $posts_count++;
                }
            }
        }
    }

    return $posts_count;
}
/**
 * Gets last post by authors trip.
 * @param array $boards Cached data.
 * @param array $author Authors data.
 * @param int $limit Upper date bound for finding last post.
 * @return array
 * 0 - url or error string if no posts found.
 * 1 - timestamp of this post.
 */
function get_last_post($boards, $author, $limit = 0) {
    $link = "no {$author['alt']} posts found or it out of top D;";
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == $author['trip']
                            && ($limit == 0 ? true : $post['date'] < $limit)
                            && $post['date'] > $ts) {

                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['date'];
                    }
                }
            }
        }
    }

    return array($link, $ts);
}
/**
 * Check if thread from cache still on top page.
 * @param array $thread Thread to check.
 * @param array $top_threads Threads from top page.
 * @return boolean
 * TRUE if thread still on top, FALSE otherwise.
 */
function is_thread_on_top($thread, $top_threads) {
    foreach ($top_threads as $t) {
        if ($t['board'] == $thread['board']
                && $t['number'] == $thread['number']) {

            return TRUE;
        }
    }

    return FALSE;
}
/**
 * Updates boards list.
 * @param array $boards Old boards list.
 */
function update_boards_list(&$boards) {
    foreach (get_boards() as $new_board) {
        $found = FALSE;
        foreach ($boards as $old_board) {
            if ($new_board['name'] == $old_board['name']) {
                $found = TRUE;
                break;
            }
        }
        if (!$found) {
            array_push($boards, $new_board);
        }
    }
}

function get_last_catie_post_link($boards) {
    $link = 'no Catie posts found or it out of cache D;';
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == '?'
                            && $post['date'] > $ts) {

                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['number'];
                    }
                }
            }
        }
    }

    return $link;
}
function get_last_pocky_post_link($boards) {
    $link = 'no Pocky posts found or it out of cache D;';
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == '!<b>pocky.</b>'
                            && $post['date'] > $ts) {

                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['date'];
                    }
                }
            }
        }
    }

    return $link;
}
function get_last_boxxmom_post_link($boards) {
    $link = 'no Boxxmom posts found or it out of top D;';
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == '!<b>boxxmom.</b>'
                            && $post['date'] > $ts) {

                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['date'];
                    }
                }
            }
        }
    }

    return $link;
}
function get_last_nozzy_post_link($boards) {
    $link = 'no Nozzy posts found or it out of cache D;';
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == '!ANOZzYuhDc'  && $post['date'] > $ts) {
                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['date'];
                    }
                }
            }
        }
    }

    return $link;
}
function get_last_crisserz_post_link($boards) {
    $link = 'no Crisserz posts found or it out of cache D;';
    $ts = 0;

    foreach ($boards as $board) {
        foreach ($board['pages'] as $page) {
            foreach ($page['threads'] as $thread) {
                foreach ($thread['posts'] as $post) {
                    if ($post['trip'] == '!!SwAmL3ZGyw'  && $post['date'] > $ts) {
                        $link = "<a href=\"http://chan.catiewayne.com/"
                               ."{$board['name']}/res/{$thread['number']}.html"
                               ."#{$post['number']}\" target=\"_blank\" "
                               ."title=\"Will open in new tab/window\">"
                               ."&gt;&gt;&gt;/{$board['name']}/"
                               ."{$post['number']}</a>";
                        $ts = $post['date'];
                    }
                }
            }
        }
    }

    return $link;
}
function get_pages($html) {
    /*
     * This fuction parst board's TOP PAGE (0) and return pages of it.
     */

    $pages = array();

    preg_match_all('/&#91;<a(.+)a>&#93;/',
                   preg_replace("/[\n\r\t]/", '', $html),
                   $matches);
    $split = preg_split('/"(\/\\w+\/)(\\d+).html"/',
                        $matches[1][0],
                        -1,
                        PREG_SPLIT_DELIM_CAPTURE);
    array_push($pages,
               array('number' => '0',
                     'link' => "http://chan.catiewayne.com{$split[1]}"));
    // 1, 4, 7, 10, ...
    for ($i = 1; $i < count($split); $i += 3) {
        array_push($pages,
                   array('number' => $split[$i + 1],
                         'link' => "http://chan.catiewayne.com"
                                  ."{$split[$i]}{$split[$i + 1]}.html"));
    }

    return $pages;
}

/***********
 * Checker *
 ***********/

include $CACHE_FILE_PATH;

set_time_limit(0);
date_default_timezone_set('America/Los_Angeles');

$boards_in_cache = 0;
if (isset($boards)) {
    $boards_in_cache = count($boards);
}
$bytes_downloaded = 0;
$start_date = new DateTime();

while (TRUE) {
    echo cur_date() . "Check time is come.\n";
    
    if ($boards_in_cache == 0) {
        echo cur_date() . "{$TAB}no boards in cache! Getting boards...\n";
        $boards = get_boards();
    } else {
        update_boards_list($boards);
    }
    echo cur_date() . "{$TAB}boards found: " . count($boards) . "\n";
    
    foreach ($boards as &$board) {
        echo cur_date() . " {$TAB}processing board: {$board['name']}...\n";

        $threads_added = 0;
        $posts_added = 0;

        // url_get_contents() delay it's own execution.
        echo cur_date() . "{$TAB}{$TAB}downloading: {$board['link']} ...\n";
        $html = url_get_contents($board['link']);
        $bytes_downloaded += strlen($html);

        $new_threads = parse_board_preview_page($html);
        list($ta, $pa) = add_new_threads($board['pages'][0]['threads'],
                                         $new_threads);
        $threads_added += $ta;
        $posts_added += $pa;

        for ($i = 0; $i < count($board['pages'][0]['threads']); $i++) {
            $thread = $board['pages'][0]['threads'][$i];

            if (!is_thread_on_top($thread, $new_threads)) {
                continue;
            }

            // Count of Visible post per thread on board page.
            $vcount = isset($thread['sticked']) && $thread['sticked'] ? 2 : 4;

            // Count of Cached posts.
            $ccount = count($thread['posts']);

            // Actual count of posts in thread.
            $pcount = $vcount + $thread['omitedn'];

            // If we count po posts in cache not equal actual count of posts
            // in thread we need recache this thread.
            if ($thread['omited'] && $ccount != $pcount) {
                echo cur_date() ."{$TAB}{$TAB}thread={$thread['number']},"
                                ."omited={$thread['omited']},"
                                ."visible=$vcount,"
                                ."omitedn={$thread['omitedn']},"
                                ."cached=$ccount,"
                                ."actual=$pcount\n";
            }
            if ($thread['omited'] && $ccount != $pcount) {
                echo cur_date() ."{$TAB}{$TAB}downloading: "
                                ."{$thread['link']} ...\n";
                $html = url_get_contents($thread['link']);
                $bytes_downloaded += strlen($html);

                $new_posts = parse_thread_view_page($html);

                // Wipe out old cached posts.
                $board['pages'][0]['threads'][$i]['posts'] = array();

                // TODO: At this place we miss original post. Because we wipe it
                // and add_new_posts() not adds original post.
                $posts = &$board['pages'][0]['threads'][$i]['posts'];
                $posts_added += add_new_posts($posts, $new_posts);
            }
        }

        echo cur_date() . "{$TAB}{$TAB}threads added: $threads_added\n";
        echo cur_date() . "{$TAB}{$TAB}posts added: $posts_added\n";
    }

    $boards_in_cache = count($boards);

    // Put updated data to cache.
    file_put_contents($CACHE_FILE_PATH,
                      "<?php\n\$boards = ".var_export($boards, TRUE).";\n?>");

    // Collect data for out html.

    $kbytes_downloaded = intval($bytes_downloaded / 1024);
    echo cur_date() ."{$TAB}{$TAB}total Kbytes "
                    ."downloaded: $kbytes_downloaded\n";

    $posts_count = get_posts_count($boards);
    $cur_date = cur_date();
    $next_upd = date("r ", time() + $UPDATE_RATE_MIN * 60);
    $uptime_diff = $start_date->diff(new DateTime());
    $uptime = $uptime_diff->format('%y:%m:%d:%h:%i:%s');
    $last_catie_post_link = get_last_catie_post_link($boards);
    $last_pocky_post_link = get_last_pocky_post_link($boards);
    $last_boxxmom_post_link = get_last_boxxmom_post_link($boards);

// Generate out html.
$OUT_HTML_CODE = "<html>
<head>
    <title>Catie Checker v2</title>
    <meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"UTF-8\">
    <link rel=\"icon\" type=\"image/png\" href=\"favicon.png\">
    <style type=\"text/css\">
        a:link {color:#B6FF00;}
        a:hover {color:#B6FF00;}
        a:active {color:#B6FF00;}
        a:visited {color:#4A5B20;}
    </style>
    <script type=\"text/javascript\">
        function toggle_display(id) {
            var elem = document.getElementById(id)
            if (elem.style.display == 'none') {
                elem.style.display = 'block'
            } else {
                elem.style.display = 'none'
            }
        }
    </script>
</head>
<body style=\"background-color:black;
              color:white;\">
    <img src=\"img/boxxy.jpg\" alt=\"boxxy.jpg\"><br>
    <b><u>Catie Checker v2</u></b><br>
    <br>
    Too busy to F5? No internet at work?<br>
    Boss/netadmin gonna mad from your Boxxy love?<br>
    This page is foar yew! RAWR!<br>
    <br>
    <b>Update rate:</b> each $UPDATE_RATE_MIN minute(s)<br>
    <b>Uptime:</b> $uptime<br>
    <b>Last update:</b> $cur_date<br>
    <b>Next update:</b> $next_upd<br>
    <b>Posts checked:</b> $posts_count<br>
    <b>Total data downloaded:</b> {$kbytes_downloaded}K<br>
    <br>";
foreach ($AUTHORS as $author) {
    $last_posts = array();
    $last_post = get_last_post($boards, $author);
    array_push($last_posts, $last_post);
    for ($i = 1; $i < 3; $i++) {
        if ($last_post[1] == 0) {
            break;
        } else {
            $last_post = get_last_post($boards, $author, $last_post[1]);
            array_push($last_posts, $last_post);
        }
    }
    $last_posts = array_reverse($last_posts);

    $OUT_HTML_CODE .= "\n    <b>Last {$author['alt']} posts:</b>";
    foreach ($last_posts as $lp) {
        $OUT_HTML_CODE .= " {$lp[0]}";
    }
    $OUT_HTML_CODE .= "<br>";
}
$OUT_HTML_CODE .= "
    <br>
    Search for tripfriend last post in cache:<br>
    <form method=\"POST\" action=\"search.php\">
        <input type=\"text\" name=\"keyword\" value=\"\">
        <input type=\"submit\" value=\"Search\">
    </form>
</body>
</html>";
file_put_contents($OUT_HTML_PATH, $OUT_HTML_CODE);

    // Wait for new check.
    echo cur_date() . "{$TAB}wait $UPDATE_RATE_MIN min for new check.\n";
    sleep($UPDATE_RATE_MIN * 60);
}

exit(0);

/*<a href=\"javascript: toggle_display('namefrends')\">toggle namefrends</a>
<div id=\"namefrends\" style=\"display:none;\">
<b>Last Nozzy post:</b> $last_nozzy_post_link<br>
<b>Last Crisserz post:</b> $last_crisserz_post_link<br>
</div>*/
?>
